package com.foreknow.mapping;

import com.foreknow.db.DBManager;

import java.util.HashMap;
import java.util.Map;

public class MappingFactory {
    //单例

    //创建Map集合并向Map集合中添加xxxMapping对象
    //使用了多态： 继承或实现关系  方法重写  父类的引用指向子类的对象
    //在项目中如何使用多态：在创建的Map集合中，value可以是父类型(接口)，向Map集合中添加元素的时候可以子类的对象
    //使用多态的好处是提高了程序的灵活性

    private static MappingFactory mappingFactory = null;
    private Map<String,EntityMapping> maps = new HashMap<>();
    public static final String GUESTBOOK_MAPPING = "guestbookMapping";
    private MappingFactory(){
    }

    public static MappingFactory getInstance() {
        if (mappingFactory == null) {
            mappingFactory = new MappingFactory();
            //向Map集合中添加元素
            mappingFactory.maps.put(GUESTBOOK_MAPPING,new GuestbookMapping());
            //            mappingFactory.maps.put("userMapping",new UserMapping());
            //            mappingFactory.maps.put("xxxMapping",new xxxMapping());
        }
        return mappingFactory;
    }

    //提供方法用于从Map集合中获取某个元素
    public EntityMapping getMapping(String key) {
        return maps.get(key);
    }


}
